package me.helllp.dake.generator;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FreeMarkerUtil {
    public static void genSrc(String fileName, String templateName, Map<String,Object> dataMap) throws IOException, TemplateException {
        File file = new File(fileName);
        genSrc(file, templateName, dataMap);
    }

    public static void genSrc(File file, String templateName, Map<String,Object> dataMap) throws IOException, TemplateException {
        Template template = FreeMarkerTemplateUtils.getTemplate(templateName);
        FileOutputStream fos = new FileOutputStream(file);

        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);
    }

    public static String genSrc(String templateStr, Map<String, Object> dataMap) throws IOException, TemplateException {
        StringWriter stringWriter = new StringWriter();
        Template template = FreeMarkerTemplateUtils.getStringTemplate(templateStr);

        template.process(dataMap, stringWriter);

        return stringWriter.toString();
    }
}
